public class test01 {
    //字符串中的单词数
    public int countSegments(String s) {
        int flag = 0;
        int ans = 0;
        for(int i = 0; i < s.length(); i++){
            char ch = s.charAt(i);
            if(ch != ' ' && flag == 0){
                flag = 1;
                ans++;
            }
            if(ch == ' ')
                flag = 0;
        }
        return ans;
    }

    public static void main(String[] args) {
        test01 test01 = new test01();
        System.out.println(test01.countSegments("Hello, my name is John"));
    }
}
